package com.my.java.io.fileio;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesFileIOUtil {
	
	private static Logger log = LoggerFactory.getLogger(PropertiesFileIOUtil.class);
	
	public static void main(String[] args) {
		
		
		
	}
	
	public static String getProperties(String fileName, String key) {
		String result = null;
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(fileName));
			Properties properties = new Properties();
			properties.load(in);
			result = properties.getProperty(key);
		}
		catch (Exception e) {
			e.printStackTrace();
			log.error("读取配置内容[{}]出错：", (new StringBuilder()).append(fileName).append("-").append(key).toString(), e);
		}
		return result;
	}

	public static Map<String,String> getAllProperties(String fileName) {
		HashMap<String,String> map = new HashMap<String,String>();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(fileName));
			Properties properties = new Properties();
			properties.load(in);
			String key;
			for (Iterator<Object> it = properties.keySet().iterator(); it.hasNext(); map.put(key, properties.getProperty(key)))
				key = it.next().toString();
		}
		catch (Exception e) {
			e.printStackTrace();
			log.error("读取配置文件[{}]出错：", fileName, e);
		}
		return map;
	}

	public static String getBundleProperties(String fileName, String key) {
		String result = null;
		try {
			PropertyResourceBundle configBundle = (PropertyResourceBundle) ResourceBundle.getBundle(fileName);
			result = configBundle.getString(key);
		}
		catch (Exception e) {
			e.printStackTrace();
			log.error("读取配置内容[{}]出错：", (new StringBuilder()).append(fileName).append("-").append(key).toString(), e);
		}
		return result;
	}

	public static Map<String,String> getBundleAllProperties(String fileName) {
		HashMap<String,String> map = new HashMap<String,String>();
		try {
			PropertyResourceBundle configBundle = (PropertyResourceBundle) ResourceBundle.getBundle(fileName);
			String key;
			for (Enumeration<String> emun = configBundle.getKeys(); emun.hasMoreElements(); map.put(key, configBundle.getString(key)))
				key = (String) emun.nextElement();
		}
		catch (Exception e) {
			e.printStackTrace();
			log.error("读取配置文件[{}]出错：", fileName, e);
		}
		return map;
	}
	
	// 根据Key读取Value
	public static String GetValueByKey(String filePath, String key) {
		Properties pps = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(filePath));
			pps.load(in);
			String value = pps.getProperty(key);
			System.out.println(key + " = " + value);
			return value;
		}
		catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	// 写入Properties信息
	public static void WriteProperties(String filePath, String pKey, String pValue) throws IOException {
		Properties pps = new Properties();
		InputStream in = new FileInputStream(filePath);
		// 从输入流中读取属性列表（键和元素对）
		pps.load(in);
		// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
		// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
		OutputStream out = new FileOutputStream(filePath);
		pps.setProperty(pKey, pValue);
		// 以适合使用 load 方法加载到 Properties 表中的格式，
		// 将此 Properties 表中的属性列表（键和元素对）写入输出流
		pps.store(out, "Update " + pKey + " name");
	}

}
